{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 时间序列处理\n",
    "* 时间数据类型\n",
    "* 时间数据计算\n",
    "#### 处理dataframe时，经常会遇到时间类型数据，本次课会介绍时间序列处理的常见function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>date</th>\n",
       "      <th>ticker</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>776.60</td>\n",
       "      <td>2015-12-29</td>\n",
       "      <td>GOOG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>771.00</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>GOOG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>758.88</td>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>GOOG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>741.84</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>GOOG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>108.74</td>\n",
       "      <td>2015-12-29</td>\n",
       "      <td>AAPL</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    close        date ticker\n",
       "0  776.60  2015-12-29   GOOG\n",
       "1  771.00  2015-12-30   GOOG\n",
       "2  758.88  2015-12-31   GOOG\n",
       "3  741.84  2016-01-04   GOOG\n",
       "4  108.74  2015-12-29   AAPL"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_csv('./data/stock.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时间数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8 entries, 0 to 7\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   close   8 non-null      float64\n",
      " 1   date    8 non-null      object \n",
      " 2   ticker  8 non-null      object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 320.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2015-12-29\n",
       "1   2015-12-30\n",
       "2   2015-12-31\n",
       "3   2016-01-04\n",
       "4   2015-12-29\n",
       "5   2015-12-30\n",
       "6   2015-12-31\n",
       "7   2016-01-04\n",
       "Name: date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(df['date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv('./data/stock.csv',parse_dates=['date'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8 entries, 0 to 7\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype         \n",
      "---  ------  --------------  -----         \n",
      " 0   close   8 non-null      float64       \n",
      " 1   date    8 non-null      datetime64[ns]\n",
      " 2   ticker  8 non-null      object        \n",
      "dtypes: datetime64[ns](1), float64(1), object(1)\n",
      "memory usage: 320.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2015\n",
       "1    2015\n",
       "2    2015\n",
       "3    2016\n",
       "4    2015\n",
       "5    2015\n",
       "6    2015\n",
       "7    2016\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    12\n",
       "1    12\n",
       "2    12\n",
       "3     1\n",
       "4    12\n",
       "5    12\n",
       "6    12\n",
       "7     1\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    29\n",
       "1    30\n",
       "2    31\n",
       "3     4\n",
       "4    29\n",
       "5    30\n",
       "6    31\n",
       "7     4\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    53\n",
       "1    53\n",
       "2    53\n",
       "3     1\n",
       "4    53\n",
       "5    53\n",
       "6    53\n",
       "7     1\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.week"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    0\n",
       "4    1\n",
       "5    2\n",
       "6    3\n",
       "7    0\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.weekday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "3    0\n",
       "4    0\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "3    0\n",
       "4    0\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.minute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "3    0\n",
       "4    0\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2015-12-29\n",
       "1    2015-12-30\n",
       "2    2015-12-31\n",
       "3    2016-01-04\n",
       "4    2015-12-29\n",
       "5    2015-12-30\n",
       "6    2015-12-31\n",
       "7    2016-01-04\n",
       "Name: date, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    363\n",
       "1    364\n",
       "2    365\n",
       "3      4\n",
       "4    363\n",
       "5    364\n",
       "6    365\n",
       "7      4\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.dayofyear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    31\n",
       "1    31\n",
       "2    31\n",
       "3    31\n",
       "4    31\n",
       "5    31\n",
       "6    31\n",
       "7    31\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.days_in_month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    29\n",
       "1    30\n",
       "2    31\n",
       "3     4\n",
       "4    29\n",
       "5    30\n",
       "6    31\n",
       "7     4\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    4\n",
       "2    4\n",
       "3    1\n",
       "4    4\n",
       "5    4\n",
       "6    4\n",
       "7    1\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.quarter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    53\n",
       "1    53\n",
       "2    53\n",
       "3     1\n",
       "4    53\n",
       "5    53\n",
       "6    53\n",
       "7     1\n",
       "Name: date, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].dt.weekofyear"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时间数据计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2015-12-29\n",
       "1   2015-12-30\n",
       "2   2015-12-31\n",
       "3   2016-01-04\n",
       "4   2015-12-29\n",
       "5   2015-12-30\n",
       "6   2015-12-31\n",
       "7   2016-01-04\n",
       "Name: date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['days']=df['date'].apply(lambda x:x+datetime.timedelta(days=5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2015-12-24\n",
       "1   2015-12-25\n",
       "2   2015-12-26\n",
       "3   2015-12-30\n",
       "4   2015-12-24\n",
       "5   2015-12-25\n",
       "6   2015-12-26\n",
       "7   2015-12-30\n",
       "Name: date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'].apply(lambda x:x+datetime.timedelta(days=-5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['seconds']=df['date'].apply(lambda x:x+datetime.timedelta(seconds=5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dateutil.relativedelta import relativedelta\n",
    "df['month']=df['date'].apply(lambda x:x+relativedelta(months=5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['year']=df['date'].apply(lambda x:x+relativedelta(years=5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>date</th>\n",
       "      <th>ticker</th>\n",
       "      <th>days</th>\n",
       "      <th>seconds</th>\n",
       "      <th>month</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>776.60</td>\n",
       "      <td>2015-12-29</td>\n",
       "      <td>GOOG</td>\n",
       "      <td>2016-01-03</td>\n",
       "      <td>2015-12-29 00:00:05</td>\n",
       "      <td>2016-05-29</td>\n",
       "      <td>2020-12-29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>771.00</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>GOOG</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>2015-12-30 00:00:05</td>\n",
       "      <td>2016-05-30</td>\n",
       "      <td>2020-12-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>758.88</td>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>GOOG</td>\n",
       "      <td>2016-01-05</td>\n",
       "      <td>2015-12-31 00:00:05</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>2020-12-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>741.84</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>GOOG</td>\n",
       "      <td>2016-01-09</td>\n",
       "      <td>2016-01-04 00:00:05</td>\n",
       "      <td>2016-06-04</td>\n",
       "      <td>2021-01-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>108.74</td>\n",
       "      <td>2015-12-29</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>2016-01-03</td>\n",
       "      <td>2015-12-29 00:00:05</td>\n",
       "      <td>2016-05-29</td>\n",
       "      <td>2020-12-29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>107.32</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>2015-12-30 00:00:05</td>\n",
       "      <td>2016-05-30</td>\n",
       "      <td>2020-12-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>105.26</td>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>2016-01-05</td>\n",
       "      <td>2015-12-31 00:00:05</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>2020-12-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>105.35</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>2016-01-09</td>\n",
       "      <td>2016-01-04 00:00:05</td>\n",
       "      <td>2016-06-04</td>\n",
       "      <td>2021-01-04</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    close       date ticker       days             seconds      month  \\\n",
       "0  776.60 2015-12-29   GOOG 2016-01-03 2015-12-29 00:00:05 2016-05-29   \n",
       "1  771.00 2015-12-30   GOOG 2016-01-04 2015-12-30 00:00:05 2016-05-30   \n",
       "2  758.88 2015-12-31   GOOG 2016-01-05 2015-12-31 00:00:05 2016-05-31   \n",
       "3  741.84 2016-01-04   GOOG 2016-01-09 2016-01-04 00:00:05 2016-06-04   \n",
       "4  108.74 2015-12-29   AAPL 2016-01-03 2015-12-29 00:00:05 2016-05-29   \n",
       "5  107.32 2015-12-30   AAPL 2016-01-04 2015-12-30 00:00:05 2016-05-30   \n",
       "6  105.26 2015-12-31   AAPL 2016-01-05 2015-12-31 00:00:05 2016-05-31   \n",
       "7  105.35 2016-01-04   AAPL 2016-01-09 2016-01-04 00:00:05 2016-06-04   \n",
       "\n",
       "        year  \n",
       "0 2020-12-29  \n",
       "1 2020-12-30  \n",
       "2 2020-12-31  \n",
       "3 2021-01-04  \n",
       "4 2020-12-29  \n",
       "5 2020-12-30  \n",
       "6 2020-12-31  \n",
       "7 2021-01-04  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   5 days\n",
       "1   5 days\n",
       "2   5 days\n",
       "3   5 days\n",
       "4   5 days\n",
       "5   5 days\n",
       "6   5 days\n",
       "7   5 days\n",
       "dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['days']-df['date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    432000.0\n",
       "1    432000.0\n",
       "2    432000.0\n",
       "3    432000.0\n",
       "4    432000.0\n",
       "5    432000.0\n",
       "6    432000.0\n",
       "7    432000.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df['days']-df['date']).dt.total_seconds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5.0\n",
       "1    5.0\n",
       "2    5.0\n",
       "3    5.0\n",
       "4    5.0\n",
       "5    5.0\n",
       "6    5.0\n",
       "7    5.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df['seconds']-df['date']).dt.total_seconds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    5\n",
       "2    5\n",
       "3    5\n",
       "4    5\n",
       "5    5\n",
       "6    5\n",
       "7    5\n",
       "dtype: int32"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((df['month']-df['date']).dt.days/30).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    5\n",
       "2    5\n",
       "3    5\n",
       "4    5\n",
       "5    5\n",
       "6    5\n",
       "7    5\n",
       "dtype: int32"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((df['year']-df['date']).dt.days/365).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课后练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sp5_df = pd.read_csv(\n",
    "    'sp500.csv', usecols=['date', 'adj_close'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把date列的变为时间类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过date列，产生年，月，日 列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对 date 加 5 分钟"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "384px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
